package com.qly.jsoup.service.util;

import com.qly.jsoup.service.config.WebsiteConfig;
import com.qly.jsoup.service.model.Goods;
import com.qly.jsoup.service.model.Jobs;
import org.jsoup.Jsoup;
import org.jsoup.nodes.Document;
import org.jsoup.nodes.Element;
import org.jsoup.select.Elements;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.boot.context.properties.EnableConfigurationProperties;
import org.springframework.stereotype.Component;

import java.net.URLEncoder;
import java.text.DateFormat;
import java.text.SimpleDateFormat;
import java.util.Date;
import java.util.HashMap;
import java.util.Map;

/**
 * Created by Andy ye on 2018/8/22.
 */
@Component
@EnableConfigurationProperties(WebsiteConfig.class)
public class JsoupUtil {

    @Autowired
    WebsiteConfig websiteConfig;

    String nullString = "\t";

    public Map<String, Object> getZhilianJobsInfo(String[] area, String[] keywords, Integer maxPage) {

        Map<String, Object> jobsMap = new HashMap<String, Object>();
        Long jobsCount = 0l;

        Date date = new Date();
        DateFormat format = new SimpleDateFormat("yyyyMMdd");
        String getDate = format.format(date);

        try {

            //遍历岗位名称
            for (String job : keywords) {

                //遍历城市
                for (String city : area) {

                    //爬取数据并写到本地
                    for (int page = 1; page <= maxPage; page++) {


                        String website_url = websiteConfig.getUrl()
                                + "&" + websiteConfig.getArea() + "=" + city
                                + "&" + websiteConfig.getKeywords() + "=" + job
                                + "&" + websiteConfig.getPage() + "=" + page;

                        System.out.println("本次爬取网页链接：" + website_url);

//                        Document doc = Jsoup.connect(website_url).get();

                        String htmlContent = HttpUtils.httpGet(website_url, "UTF-8");
                        Document doc = Jsoup.parse(htmlContent);

                        Element content = doc.getElementById("listContent");
                        if (content == null) {
                            continue;
                        }

                        // 职位名称
                        Elements zwmcEls = content.getElementsByClass("jobName");
                        // 公司名称
                        Elements gsmcEls = content.getElementsByClass("commpanyName");
                        // 职位描述
                        Elements zwmsEls = content.getElementsByClass("jobDesc");
                        // 公司性质规模
                        Elements gzddEls = content.getElementsByClass("commpanyDesc");
                        // 公司福利
                        Elements welfareEls = content.getElementsByClass("job_welfare");
                        // 招聘状态
                        Elements statusEls = content.getElementsByClass("commpanyStatus");


                        for (int j = 1; j <= zwmcEls.size(); j++) {

                            Jobs jobs = new Jobs();
                            jobs.setKeywords(job);
                            jobs.setJobArea(city);
                            jobs.setJobGetDate(getDate);

                            String jobName = zwmcEls.get(j).tagName("a").text();//职位名称
                            String jobDetailUrl = zwmcEls.get(j).tagName("a").attr("href");//职位详情页
                            jobs.setJobName(jobName);
                            jobs.setJobDetailUrl(jobDetailUrl);

                            String companyName = gsmcEls.get(j).tagName("a").text();// 公司名称
                            String companyDetailUrl = gsmcEls.get(j).tagName("a").attr("href");// 公司详情页
                            jobs.setCompanyName(companyName);
                            jobs.setCompanyDetailUrl(companyDetailUrl);

                            String jobSaray = zwmsEls.get(j).tagName("p").text();// 职位月薪
                            String jobArea = zwmsEls.get(j).getElementsByClass("demand_item").first().text();// 公司行政区
                            String jobExperience = zwmsEls.get(j).getElementsByClass("demand_item").get(1).text();//经验
                            String jobLevel = zwmsEls.get(j).getElementsByClass("demand_item").last().text();//学历
                            jobs.setJobSaray(jobSaray);
                            jobs.setJobArea(jobArea);
                            jobs.setJobExperience(jobExperience);
                            jobs.setJobLevel(jobLevel);

                            String jobProperty = gzddEls.get(j).getElementsByClass("info_item").first().text();// 公司性质
                            jobs.setJobProperty(jobProperty);

                            String jobWelfare = welfareEls.get(j).getElementsByClass("welfare_item").text();// 公司福利
                            jobs.setJobWelfare(jobWelfare);

                            String jobStatus = statusEls.get(j).tagName("span").text();// 招聘状态
                            jobs.setJobStatus(jobStatus);

                            jobsMap.put(jobName, jobs);//岗位与岗位详细信息
                        }

                        jobsCount++;
                    }

                }
            }

            jobsMap.put("jobsCount", jobsCount);//爬取岗位总条数

        } catch (Exception e) {
            e.printStackTrace();
        }

        return jobsMap;

    }


    public String getZhilianJobsJson(String[] area, String[] jobName, Integer maxPage) {

        Map<String, Object> rst = new HashMap<String, Object>();

        String url = "https://fe-api.zhaopin.com/c/i/sou?pageSize=60&kt=3&jobWelfareTag=-1&employmentType=-1&companyType=-1&education=-1&workExperience=-1";
        url += "&cityId=489";
        url += "&kw=java";
        url += "&p=2";

        return HttpUtils.httpGet(url, "UTF-8");
    }


    public Map<String, Object> getVipComList() {

        Map<String, Object> resultMap = new HashMap<String, Object>();

        String url = "https://category.vip.com/suggest.php?keyword=" + URLEncoder.encode("连衣裙") + "&ff=" + URLEncoder.encode("235|12|1|1");

        String htmlContent = HttpUtils.httpGet(url, "UTF-8");
        Document doc = Jsoup.parse(htmlContent);

        Element content = doc.getElementById("J_searchCatList");
        if (content == null) {
            resultMap.put("msgCode", "no goods");
            return resultMap;
        }

        Elements item = doc.getElementsByClass("goods-list-item");
        for (int j = 1; j <= item.size(); j++) {

            Goods goods = new Goods();

            String goods_id = item.get(j).attr("data-id");
            String brand_id = item.get(j).attr("data-brand");
            goods.setGoodsId(goods_id);
            goods.setBrandId(brand_id);

            // 商品名称
            Elements mc = item.get(j).getElementsByClass("goods-title-info");
            // 折扣价
            Elements zkj = content.getElementsByClass("price");
            // 原价
            Elements yj = content.getElementsByClass("goods-market-price");
            // 折扣
            Elements zk = content.getElementsByClass("goods-discount");
            // 优惠信息
            Elements yh = content.getElementsByClass("good-title-pms");
            // 商品图片
            Elements tp = content.getElementsByClass("goods-image");

            String goods_name = mc.tagName("a").text();
            String goods_url = mc.tagName("a").attr("href");
            String goods_brand = mc.tagName("a").tagName("span").text();
            goods.setGoodsName(goods_name);
            goods.setGoodsUrl(goods_url);
            goods.setGoodsBrand(goods_brand);

            String goods_price = zkj.text();
            String goods_origin_price = yj.text();
            String goods_discount = zk.text();

            goods.setGoodsPrice(goods_price);
            goods.setGoodsOriginPrice(goods_origin_price);
            goods.setGoodsDiscount(goods_discount);

            String goods_welfair = yh.attr("title");
            String goods_image = tp.tagName("a").tagName("img").attr("src");
            goods.setGoodsWelfair(goods_welfair);
            goods.setGoodsImage(goods_image);

            resultMap.put(goods_id, goods);

        }

        return resultMap;

    }
}

